package com.ruiyangwang.ListenBook.servlet.user;

import com.ruiyangwang.ListenBook.service.UserService;
import com.ruiyangwang.ListenBook.viewObject.UserV0;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author syzygy
 * @date 2022/07/11 18:10
 **/
@WebServlet("/studio/user/login.do")
public class LoginDoServlet extends HttpServlet {
    private final UserService userService = new UserService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //登录
        UserV0 userV0 = userService.login(username,password);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain");
        PrintWriter writer = resp.getWriter();
        if(userV0 == null){
            writer.println("登录失败");
        }else{
            HttpSession session = req.getSession();
            session.setAttribute("currentUser",userV0);
            writer.println("欢迎" + username + "用户，登录成功");
        }
        resp.sendRedirect("/studio");


    }
}